<!-- //这是一个根据ID查询用户信息的页面 -->




<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>用户信息查询系统</title>
    <style>
        body {
            font-family: 'Segoe UI', Arial, sans-serif;
            max-width: 800px;
            margin: 2rem auto;
            padding: 0 20px;
        }
        .search-box {
            background: #f5f5f5;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        input[type="text"] {
            width: 60%;
            padding: 10px;
            margin-right: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        button {
            padding: 10px 20px;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background 0.3s;
        }
        button:hover {
            background: #0056b3;
        }
        .result {
            margin-top: 2rem;
            padding: 1rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            display: none;
        }
        .loading {
            display: none;
            color: #666;
            margin-top: 1rem;
        }
        .error {
            color: #dc3545;
            margin-top: 1rem;
        }
    </style>
</head>
<body>
    <div class="search-box">
        <h1>用户信息查询</h1>
        <div>
            <input type="text" id="userId" placeholder="请输入用户ID">
            <button onclick="searchUser()">查询</button>
        </div>
        <div class="loading" id="loading">加载中...</div>
        <div class="error" id="error"></div>
        <div class="result" id="result">
            <h3>用户信息</h3>
            <p>ID: <span id="result-id"></span></p>
            <p>用户名: <span id="result-username"></span></p>
            <p>年龄: <span id="result-age"></span></p>
        </div>
    </div>

    <script>
        function searchUser() {
            const userId = document.getElementById('userId').value;
            const resultDiv = document.getElementById('result');
            const errorDiv = document.getElementById('error');
            const loadingDiv = document.getElementById('loading');

            if (!userId) {
                errorDiv.textContent = "请输入有效的用户ID";
                resultDiv.style.display = 'none';
                return;
            }

            // 显示加载状态
            loadingDiv.style.display = 'block';
            errorDiv.textContent = '';
            resultDiv.style.display = 'none';

            // 调用后端API（假设API地址为 /api/user/{id}）
            fetch(`/api/user/${userId}`)
                .then(response => {
                    loadingDiv.style.display = 'none';
                    if (!response.ok) {
                        throw new Error(response.status === 404 ? '用户不存在' : '查询失败');
                    }
                    return response.json();
                })
                .then(data => {
                    document.getElementById('result-id').textContent = data.id;
                    document.getElementById('result-username').textContent = data.username;
                    document.getElementById('result-age').textContent = data.age;
                    resultDiv.style.display = 'block';
                })
                .catch(error => {
                    errorDiv.textContent = error.message;
                    resultDiv.style.display = 'none';
                });
        }
    </script>
</body>
</html>